import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../models/drama_detail_model.dart';
import '../../utils/http_util.dart';

class DetailViewmodel extends GetxController {
  String dramaId = '0';
  var titleIndex = 0.obs;
  var titles = ['短剧介绍', '艺人'].obs;
  var detailModel = Rxn<DramaDetailModel>(null);
  var showBuy = false.obs;
  var buyCount = 1.obs;
  var isKeyBoardShow = false.obs;
  final TextEditingController editTextController = TextEditingController(
    text: '1',
  );

  changeIndex(int index) {
    titleIndex.value = index;
  }

  changeShowBuy() {
    showBuy.value = !showBuy.value;
    if(!showBuy.value){
      buyCount.value = 1;
      editTextController.text = '1';
    }
  }

  Future<void> queryDetail() async {
    EasyLoading.show(status: 'loading', maskType: EasyLoadingMaskType.clear);
    try {
      final result = await HttpUtil().get(
        'drama/info',
        params: {'drama_id': dramaId},
      );
      final dynamic responseData = result.data['data']; // 明确类型声明
      detailModel.value = DramaDetailModel.fromJson(responseData);
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
    EasyLoading.dismiss();
  }

  Future<void> queryPurchase(String dramaId, String count) async {
    EasyLoading.show(status: 'loading', maskType: EasyLoadingMaskType.clear);
    try {
      final result = await HttpUtil().post(
        'drama/order',
        params: {"drama_id": dramaId, "total_num": count},
      );
      final dynamic responseData = result.data['code']; // 明确类型声明
      if(responseData==1){
        EasyLoading.showToast('购买成功');
        Timer(Duration(milliseconds: 500), (){
          showBuy.value = false;
          buyCount.value = 1;
          editTextController.text = '1';
        });
      }else{
        EasyLoading.showToast(result.data['msg']);
      }
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
    EasyLoading.dismiss();
  }
}
